﻿using DevExpress.XtraGrid.Views.BandedGrid;
using DLL;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Linq;

namespace NursingFormSystem.RiskAssessment
{
    public partial class HistoryRiskSoreNursing : DevExpress.XtraEditors.XtraForm
    {
        //窗体构造函数
        Tb_User LoginUser;
        NursingFormRecord record = new NursingFormRecord();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo Patientinfo;//选前选中患者信息 传入调用的form中
        public HistoryRiskSoreNursing(Tb_User loginUser, PatientInfo patientinfo)
        {
            InitializeComponent();
            Patientinfo = patientinfo;
            LoginUser = loginUser;
            BindControl();
            BindInfo();
        }
        private void BindControl()
        {
            BeginDate.DateTime = DateTime.Now.AddDays(-30).Date;
            endDate.DateTime = DateTime.Now;
            foreach (GridBand gridBand in this.bandedGridView1.Bands)//设置居中
            {
                foreach (GridBand gridBandSecond in gridBand.Children)
                {
                    gridBandSecond.OptionsBand.FixedWidth = true;
                    gridBandSecond.AppearanceHeader.Options.UseTextOptions = true;
                    gridBandSecond.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                    gridBandSecond.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
                }
                gridBand.OptionsBand.FixedWidth = true;
                gridBand.AppearanceHeader.Options.UseTextOptions = true;
                gridBand.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gridBand.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            }

        }

        #region  显示,共用方法
        private void BindInfo()
        {
            if (this.Patientinfo == null) return;
            string patientid = this.Patientinfo.PATIENT_ID;//住院号
            int VisitId = Patientinfo.VISIT_ID;//住院次数
            int FormId = (int)FormType.RiskSore;
            DataTable dt = new DataTable();
            #region 列头
            dt.Columns.Add("Id", typeof(string));
            dt.Columns.Add("PingGuRiQi", typeof(string));
            dt.Columns.Add("ZongFen", typeof(string));
            dt.Columns.Add("GanJue1", typeof(string));
            dt.Columns.Add("GanJue2", typeof(string));
            dt.Columns.Add("GanJue3", typeof(string));
            dt.Columns.Add("GanJue4", typeof(string));
            dt.Columns.Add("ChaoShi1", typeof(string));
            dt.Columns.Add("ChaoShi2", typeof(string));
            dt.Columns.Add("ChaoShi3", typeof(string));
            dt.Columns.Add("ChaoShi4", typeof(string));
            dt.Columns.Add("HuoDongLi1", typeof(string));
            dt.Columns.Add("HuoDongLi2", typeof(string));
            dt.Columns.Add("HuoDongLi3", typeof(string));
            dt.Columns.Add("HuoDongLi4", typeof(string));
            dt.Columns.Add("YiDongLi1", typeof(string));
            dt.Columns.Add("YiDongLi2", typeof(string));
            dt.Columns.Add("YiDongLi3", typeof(string));
            dt.Columns.Add("YiDongLi4", typeof(string));
            dt.Columns.Add("MoChaLiHeJianQieLi1", typeof(string));
            dt.Columns.Add("MoChaLiHeJianQieLi2", typeof(string));
            dt.Columns.Add("MoChaLiHeJianQieLi3", typeof(string));
            dt.Columns.Add("YingYang1", typeof(string));
            dt.Columns.Add("YingYang2", typeof(string));
            dt.Columns.Add("YingYang3", typeof(string));
            dt.Columns.Add("YingYang4", typeof(string));
            dt.Columns.Add("TiWeiAnZhiYuBianHuan1", typeof(string));
            dt.Columns.Add("TiWeiAnZhiYuBianHuan2", typeof(string));
            dt.Columns.Add("TiWeiAnZhiYuBianHuan3", typeof(string));
            dt.Columns.Add("TiWeiAnZhiYuBianHuan4", typeof(string));
            dt.Columns.Add("TiWeiAnZhiYuBianHuan5", typeof(string));
            dt.Columns.Add("JianYaYongJuYuShiYong1", typeof(string));
            dt.Columns.Add("JianYaYongJuYuShiYong2", typeof(string));
            dt.Columns.Add("JianYaYongJuYuShiYong3", typeof(string));
            dt.Columns.Add("JianYaYongJuYuShiYong4", typeof(string));
            dt.Columns.Add("PiFuHuLi1", typeof(string));
            dt.Columns.Add("PiFuHuLi2", typeof(string));
            dt.Columns.Add("PiFuHuLi3", typeof(string));
            dt.Columns.Add("PiFuHuLi4", typeof(string));
            dt.Columns.Add("PiFuHuLi5", typeof(string));
            dt.Columns.Add("PiFuHuLi6", typeof(string));
            dt.Columns.Add("YingYangZhiChi1", typeof(string));
            dt.Columns.Add("YingYangZhiChi2", typeof(string));
            dt.Columns.Add("YingYangZhiChi3", typeof(string));
            dt.Columns.Add("YingYangZhiChi4", typeof(string));
            dt.Columns.Add("YingYangZhiChi5", typeof(string));
            dt.Columns.Add("HuShiQianMing", typeof(string));
            dt.Columns.Add("HuShiZhangQianMing", typeof(string));
            dt.Columns.Add("YuHou1", typeof(string));
            dt.Columns.Add("YuHou2", typeof(string));
            dt.Columns.Add("YuHou3", typeof(string));
            dt.Columns.Add("YuHou4", typeof(string));
            dt.Columns.Add("YuHou5", typeof(string));
            dt.Columns.Add("YuHou6", typeof(string));
            dt.Columns.Add("HuanZheQianZi", typeof(string));
            dt.Columns.Add("YuHuanZheGuanXi", typeof(string));
            dt.Columns.Add("HuanZhePiFuQingGuang", typeof(string));
            dt.Columns.Add("TiWeiAnZhiYuBianHuan6", typeof(string));
            #endregion
            var ret = formService.GetNursingFormRecordByDate(patientid, VisitId, FormId, this.BeginDate.DateTime, this.endDate.DateTime);
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    RiskSore bg = new RiskSore();
                    bg = XmlUtil.XMLToObject<RiskSore>(NursingFormData, Encoding.UTF8);
                    //单项选中编号
                    string GanJueNumber = bg.GanJue;
                    string ChaoShiNumber = bg.ChaoShi;
                    string HuoDongLiNumber = bg.HuoDongLi;
                    string YiDongLiNumber = bg.YiDongLi;
                    string MoChaLiHeJianQieLiNumber = bg.MoChaLiHeJianQieLi;
                    string YingYangNumber = bg.YingYang;

                    DataRow newRow = dt.NewRow();
                    if (!string.IsNullOrEmpty(GanJueNumber))
                    {
                        newRow["GanJue" + GanJueNumber] = GanJueNumber;
                    }
                    if (!string.IsNullOrEmpty(ChaoShiNumber))
                    {
                        newRow["ChaoShi" + ChaoShiNumber] = ChaoShiNumber;
                    }
                    if (!string.IsNullOrEmpty(HuoDongLiNumber))
                    {
                        newRow["HuoDongLi" + HuoDongLiNumber] = HuoDongLiNumber;
                    }
                    if (!string.IsNullOrEmpty(YiDongLiNumber))
                    {
                        newRow["YiDongLi" + YiDongLiNumber] = YiDongLiNumber;
                    }
                    if (!string.IsNullOrEmpty(MoChaLiHeJianQieLiNumber))
                    {
                        newRow["MoChaLiHeJianQieLi" + MoChaLiHeJianQieLiNumber] = MoChaLiHeJianQieLiNumber;
                    }
                    if (!string.IsNullOrEmpty(YingYangNumber))
                    {
                        newRow["YingYang" + YingYangNumber] = YingYangNumber;
                    }

                    //复选框-体位安置与变换
                    string TiWeiAnZhiYuBianHuan = bg.TiWeiAnZhiYuBianHuan;
                    string[] TiArray;
                    if (!string.IsNullOrEmpty(TiWeiAnZhiYuBianHuan))
                    {
                        TiArray = TiWeiAnZhiYuBianHuan.Split('#');
                        if (Convert.ToInt32(TiArray[0]) == 1)
                        {
                            newRow["TiWeiAnZhiYuBianHuan1"] = "√";
                        }
                        if (Convert.ToInt32(TiArray[1]) == 1)
                        {
                            newRow["TiWeiAnZhiYuBianHuan2"] = "√";
                        }
                        if (Convert.ToInt32(TiArray[2]) == 1)
                        {
                            newRow["TiWeiAnZhiYuBianHuan3"] = "√";
                        }
                        if (Convert.ToInt32(TiArray[3]) == 1)
                        {
                            newRow["TiWeiAnZhiYuBianHuan4"] = "√";
                        }
                        if (Convert.ToInt32(TiArray[4]) == 1)
                        {
                            newRow["TiWeiAnZhiYuBianHuan5"] = "√";
                        }
                        if (Convert.ToInt32(TiArray[5]) == 1)
                        {
                            newRow["TiWeiAnZhiYuBianHuan6"] = "√";
                        }
                    }
                    //复选框-减压用具与使用
                    string JianYaYongJuYuShiYong = bg.JianYaYongJuYuShiYong;
                    string[] JianArray = JianYaYongJuYuShiYong.Split('#');
                    if (Convert.ToInt32(JianArray[0]) == 1)
                    {
                        newRow["JianYaYongJuYuShiYong1"] = "√";
                    }
                    if (Convert.ToInt32(JianArray[1]) == 1)
                    {
                        newRow["JianYaYongJuYuShiYong2"] = "√";
                    }
                    if (Convert.ToInt32(JianArray[2]) == 1)
                    {
                        newRow["JianYaYongJuYuShiYong3"] = "√";
                    }
                    if (Convert.ToInt32(JianArray[3]) == 1)
                    {
                        newRow["JianYaYongJuYuShiYong4"] = "√";
                    }
                    //复选框-皮肤护理
                    string PiFuHuLi = bg.PiFuHuLi;
                    string[] PiArray = PiFuHuLi.Split('#');
                    if (Convert.ToInt32(PiArray[0]) == 1)
                    {
                        newRow["PiFuHuLi1"] = "√";
                    }
                    if (Convert.ToInt32(PiArray[1]) == 1)
                    {
                        newRow["PiFuHuLi2"] = "√";
                    }
                    if (Convert.ToInt32(PiArray[2]) == 1)
                    {
                        newRow["PiFuHuLi3"] = "√";
                    }
                    if (Convert.ToInt32(PiArray[3]) == 1)
                    {
                        newRow["PiFuHuLi4"] = "√";
                    }
                    if (Convert.ToInt32(PiArray[4]) == 1)
                    {
                        newRow["PiFuHuLi5"] = "√";
                    }
                    if (Convert.ToInt32(PiArray[5]) == 1)
                    {
                        newRow["PiFuHuLi6"] = "√";
                    }
                    //复选框-营养支持
                    string YingYangZhiChi = bg.YingYangZhiChi;
                    string[] YingArray = YingYangZhiChi.Split('#');
                    if (Convert.ToInt32(YingArray[0]) == 1)
                    {
                        newRow["YingYangZhiChi1"] = "√";
                    }
                    if (Convert.ToInt32(YingArray[1]) == 1)
                    {
                        newRow["YingYangZhiChi2"] = "√";
                    }
                    if (Convert.ToInt32(YingArray[2]) == 1)
                    {
                        newRow["YingYangZhiChi3"] = "√";
                    }
                    if (Convert.ToInt32(YingArray[3]) == 1)
                    {
                        newRow["YingYangZhiChi4"] = "√";
                    }
                    //复选框-预后
                    string YuHou = bg.YuHou;
                    string[] YuArray = YuHou.Split('#');
                    if (Convert.ToInt32(YuArray[0]) == 1)
                    {
                        newRow["YuHou1"] = "√";
                    }
                    if (Convert.ToInt32(YuArray[1]) == 1)
                    {
                        newRow["YuHou2"] = "√";
                    }
                    if (Convert.ToInt32(YuArray[2]) == 1)
                    {
                        newRow["YuHou3"] = "√";
                    }
                    if (Convert.ToInt32(YuArray[3]) == 1)
                    {
                        newRow["YuHou4"] = "√";
                    }
                    if (Convert.ToInt32(YuArray[4]) == 1)
                    {
                        newRow["YuHou5"] = "√";
                    }
                    if (Convert.ToInt32(YuArray[5]) == 1)
                    {
                        newRow["YuHou6"] = "√";
                    }
                    newRow["Id"] = record.Id;
                    newRow["PingGuRiQi"] = Convert.ToDateTime(bg.PingGuRiQi).ToString("yyyy-MM-dd");
                    newRow["ZongFen"] = bg.ZongFen;
                    newRow["HuanZheQianZi"] = bg.HuanZheQianZi;
                    newRow["YuHuanZheGuanXi"] = bg.YuHuanZheGuanXi;
                    newRow["HuanZhePiFuQingGuang"] = bg.HuanZhePiFuQingGuang;
                    newRow["HuShiZhangQianMing"] = bg.HuShiZhangQianMing;
                    newRow["HuShiQianMing"] = bg.HuShiQianMing;
                    dt.Rows.Add(newRow);
                }
            }
            gridControl1.DataSource = dt;
        }
        #endregion

        //新增
        private void btn_AddRecord_Click(object sender, EventArgs e)
        {
            AddRiskSoreNursing addForm = new AddRiskSoreNursing(LoginUser, Patientinfo, string.Empty);
            addForm.ShowDialog();
            if (addForm.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }
        }

        private void bandedGridView1_DoubleClick(object sender, EventArgs e)
        {
            ShowEditForm();
        }

        private void ShowEditForm()
        {
            if (bandedGridView1.RowCount > 0)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                AddRiskSoreNursing addForm = new AddRiskSoreNursing(LoginUser, Patientinfo, Id);
                addForm.ShowDialog();
                if (addForm.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }

        //查询
        private void btn_Query_Click(object sender, EventArgs e)
        {
            BindInfo();
        }

        //修改
        private void Btn_Edit_Click(object sender, EventArgs e)
        {
            ShowEditForm();
        }

        //打印
        private void btn_Printing_Click(object sender, EventArgs e)
        {
            RiskPrint print = new RiskPrint(LoginUser, Patientinfo, BeginDate.DateTime, endDate.DateTime);
            print.ShowDialog();
        }

        //删除
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount <= 0) return;
            if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                var ret2 = formService.DeleteNursingFormRecord(id);
                MessageBox.Show(ret2.M);
                if (ret2.Code == 1)
                {
                    BindInfo();
                }
            }
        }
    }
}